package club.hicode.daydayup.nio;

import java.net.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;

/**
 * TCP Server
 *
 * @author Liu Chunfu
 * @date 2018-04-19 下午7:25
 **/
public class TcpServer {
    private static final int SERVERPORT = 8888;
    private ServerSocket MyServer = null;
    private List<Socket> Clients = new ArrayList<Socket>();
    private ExecutorService mExecutorService;

    public TcpServer() {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        try {
            MyServer = new ServerSocket(SERVERPORT);
            mExecutorService = Executors.newCachedThreadPool();
            System.out.println("start:");
            Socket MySocket = null;
            while (true) {
                MySocket = MyServer.accept();
                Clients.add(MySocket);
                mExecutorService.execute(new ThreadServer(MySocket));
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

    class ThreadServer implements Runnable {
        private Socket msocket = null;
        private BufferedReader in = null;
        private PrintWriter out = null;
        private String mStrMSG = null;

        public ThreadServer(Socket socket) {
            try {
                this.msocket = socket;
                in = new BufferedReader(new InputStreamReader(msocket.getInputStream(), "GB2312"));
                mStrMSG = "user:" + msocket.getInetAddress() + " come total:" + Clients.size();
                sendMassage();
            } catch (IOException e) {
                System.out.println("erorr");
                System.exit(0);
            }
        }


        @Override
        public void run() {
            try {
                while ((mStrMSG = in.readLine()) != null) {
                    if (mStrMSG.trim().equals("exit")) {
                        Clients.remove(msocket);
                        in.close();
                        out.close();
                        mStrMSG = "user:" + msocket.getInetAddress() + " exit total:" + Clients.size();
                        ;
                        msocket.close();
                        sendMassage();
                        break;
                    } else {
                        mStrMSG = msocket.getInetAddress() + ":" + mStrMSG;
                        sendMassage();
                    }

                }
            } catch (IOException e) {
                System.out.println("erorr");
                System.exit(0);
            }
        }

        private void sendMassage() {
            try {
                System.out.println(mStrMSG);
                for (Socket MySocket : Clients) {
                    out = new PrintWriter(new OutputStreamWriter(MySocket.getOutputStream(), "GB2312"), true);
                    out.println(mStrMSG);
                }
            } catch (IOException e) {
                System.out.println("error");
                System.exit(0);
            }
        }

    }

    public static void main(String[] args) {
        new TcpServer();
    }
}